{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Plotting Pandapower Networks Without Geographical Information"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If a network does not have geographic coordinates, you can create generic coordinates for plotting with the create_generic_coordinates function.\n",
    "\n",
    "###### You need to install the igraph package for this functionality: https://igraph.org/python/"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T11:37:54.507195Z",
     "start_time": "2025-10-20T11:37:54.480698Z"
    }
   },
   "outputs": [],
   "source": [
    "from pandapower.networks import mv_oberrhein\n",
    "from pandapower.plotting.generic_geodata import create_generic_coordinates, fuse_geodata\n",
    "from pandapower.plotting.collections import create_bus_collection, create_line_collection, create_trafo_collection, draw_collections\n",
    "from pandapower.plotting.plotting_toolbox import get_collection_sizes\n",
    "import warnings\n",
    "warnings.simplefilter(action='ignore', category=FutureWarning)\n",
    "%matplotlib inline\n",
    "try:\n",
    "    from seaborn import color_palette\n",
    "    colors = color_palette()\n",
    "except ImportError:\n",
    "    colors = [\"b\", \"g\", \"r\", \"c\", \"y\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We delete the geocoordinates from the network and create generic ones:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T11:38:01.290987Z",
     "start_time": "2025-10-20T11:37:54.592188Z"
    }
   },
   "outputs": [],
   "source": [
    "net = mv_oberrhein()\n",
    "# net.bus.geo.drop(net.bus.geo.index, inplace=True)\n",
    "net.bus.geo = None\n",
    "# net.line.geo.drop(net.line.geo.index, inplace=True)\n",
    "net.line.geo = None\n",
    "create_generic_coordinates(net, respect_switches=False) #create artificial coordinates with the igraph package"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "As you can see the table bus_geodata has been created and we can now plot as before. Since the function only creates bus geodata, we can only use the direct line plotting. Furthermore it creates a distance between high- and low voltage bus of a transformer, which is why we also need a transformer collection:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T11:38:01.750781Z",
     "start_time": "2025-10-20T11:38:01.391782Z"
    }
   },
   "outputs": [],
   "source": [
    "sizes = get_collection_sizes(net)\n",
    "bc = create_bus_collection(net, net.bus.index, size=sizes['bus'], color='b', zorder=10)\n",
    "tlc, tpc = create_trafo_collection(net, net.trafo.index, color=\"g\", size=sizes['trafo'])\n",
    "lcd = create_line_collection(net, net.line.index, color=\"grey\", linewidths=0.5, use_bus_geodata=True)\n",
    "sc = create_bus_collection(net, net.ext_grid.bus.values, patch_type=\"rect\", size=sizes['ext_grid'], color=\"y\", zorder=11)\n",
    "draw_collections([lcd, bc, tlc, tpc, sc], figsize=(8,6))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The create_generic_coordinates function treats transformers as edges, which means the HV and LV side of the transformer are shown by seperate buses with a trafo symbol as connection (consisting of a line collection for the connections and a patch collection for the circles).\n",
    "\n",
    "If you do not want to plot the transformers you can use the fuse_geodata function. It fuses the geocoordinates of all buses that are geographically in one place (HV/LV bus of a transformer or buses and buses that are connected by a bus-bus switch):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T11:38:01.844329Z",
     "start_time": "2025-10-20T11:38:01.804807Z"
    },
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "net.bus.geo.loc"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T11:38:02.358937Z",
     "start_time": "2025-10-20T11:38:01.885697Z"
    }
   },
   "outputs": [],
   "source": [
    "fuse_geodata(net)\n",
    "sizes = get_collection_sizes(net)\n",
    "bc = create_bus_collection(net, net.bus.index, size=sizes['bus'], color=colors[0], zorder=10)\n",
    "tlc, tpc = create_trafo_collection(net, net.trafo.index, color=\"g\", size=sizes['trafo'])\n",
    "lcd = create_line_collection(net, net.line.index, color=\"grey\", linewidths=0.5, use_bus_geodata=True)\n",
    "sc = create_bus_collection(net, net.ext_grid.bus.values, patch_type=\"rect\", size=sizes['bus'], color=\"y\", zorder=11)\n",
    "draw_collections([lcd, bc, tlc, tpc, sc], figsize=(8,6))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Plot Structural Plans"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "To plot a structural plan of the network instead of a geographical one, call the generic coordinates function with respect_switches=True."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T11:38:07.022590Z",
     "start_time": "2025-10-20T11:38:02.424443Z"
    }
   },
   "outputs": [],
   "source": [
    "net = mv_oberrhein()\n",
    "net.bus.geo = None\n",
    "net.line.geo = None\n",
    "create_generic_coordinates(net, respect_switches=True) #create artificial coordinates with the igraph package"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In that way, the algorithm seperates buses which are seperated by an open switch:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T11:38:07.444302Z",
     "start_time": "2025-10-20T11:38:07.067873Z"
    }
   },
   "outputs": [],
   "source": [
    "fuse_geodata(net)\n",
    "bc = create_bus_collection(net, net.bus.index, size=.2, color=colors[0], zorder=10)\n",
    "tlc, tpc = create_trafo_collection(net, net.trafo.index, color=\"g\")\n",
    "lcd = create_line_collection(net, net.line.index, color=\"grey\", linewidths=0.5, use_bus_geodata=True)\n",
    "sc = create_bus_collection(net, net.ext_grid.bus.values, patch_type=\"rect\", size=.5, color=\"y\", zorder=11)\n",
    "draw_collections([lcd, bc, tlc, tpc, sc], figsize=(8,6))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For a clearer arrangement, it might be useful to only plot the lines without an open switch:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T11:38:07.789417Z",
     "start_time": "2025-10-20T11:38:07.521982Z"
    }
   },
   "outputs": [],
   "source": [
    "closed_lines = set(net.line.index) - set(net.switch[(net.switch.et==\"l\") & (net.switch.closed==False)].element.values)\n",
    "lcd = create_line_collection(net, closed_lines, color=\"grey\", linewidths=0.5, use_bus_geodata=True)\n",
    "draw_collections([lcd, bc, tlc, tpc, sc], figsize=(8,6))"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
